//
//  WHLoginController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/11.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHLoginController.h"
#import "WHLoginView.h"
#import "WHTextInfoController.h"
#import "WHForgetPasswordController.h" // 忘记密码

#define KUserLoadInfo @"KUserLoadInfo"
#define KUserPhoneNumber @"KUserPhoneNumber"

@interface WHLoginController ()<WHLoginViewDelegate>

@property (nonatomic, copy) loginBlock loginCompletion;

@end

@implementation WHLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"松鼠侠商家";
    
    self.naviType = NaviTypeClear;
    
    [self setUpUI];
    [self setLoginBlock];
}

- (void)setUpUI {
    
    WHLoginView *accountLoginView = [[WHLoginView alloc] init];
    accountLoginView.backgroundColor = CViewBgMainColor;
    [self.view addSubview:accountLoginView];
    accountLoginView.delegate = self;
    [accountLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(kRealwidth(12));
    }];
    
    YYCache *cache = [[YYCache alloc] initWithName:KUserLoadInfo];
    NSString *phoneNumber = (NSString *)[cache objectForKey:KUserPhoneNumber];
    if (phoneNumber) {
        [accountLoginView setLoginNumber:phoneNumber];
    }
}

- (void)dismissLoginModules {
    [userManager cancelRequest];
    KPostNotification(KNotificationLoginStateChange, @YES);
}

#pragma mark - —————— 事件 ——————
- (void)doLogin:(NSArray *)messageArray {
    DLog(@"messageArray = %@", messageArray);
    [self.view endEditing:YES];
    NSString *phoneNumber = messageArray[0];
    NSString *codeNumber = messageArray[1];
    BOOL is = [WHValidationHelper validateMobile:phoneNumber];
    if (!is) {
        [WHView showMessage:@"请输入正确的手机号" inView:self.view];
        return;
    }
    if (codeNumber.length == 0) {
        [WHView showMessage:@"密码不能为空" inView:self.view];
        return;
    }
    if (![WHValidationHelper validatePassword:codeNumber]) {
        [WHView showMessage:@"密码输入不合法!" inView:self.view];
        return;
    }
    
    kWeakSelf(self);
    [userManager doAccountLoginWithPhoneNumber:phoneNumber password:codeNumber completion:^(BOOL success, NSString *description) {
        if (success) {
            YYCache *cache = [YYCache cacheWithName:KUserLoadInfo];
            [cache setObject:phoneNumber forKey:KUserPhoneNumber];
        }
        weakself.loginCompletion(success, description);
    }];
}

- (void)setLoginBlock {
    kWeakSelf(self);
    self.loginCompletion = ^(BOOL success, NSString *description) {
        if (success) {
            [WHView showHudMsg:@"登录成功"];
            [weakself dismissLoginModules];
        } else {
            [WHView showHudMsg:description];
        }
    };
}

- (void)toForgetPassword {
    WHForgetPasswordController *forgetPassword = [[WHForgetPasswordController alloc] init];
    [self.navigationController pushViewController:forgetPassword animated:YES];
}

- (void)toRuleWeb {
    WHTextInfoController *textInfo = [[WHTextInfoController alloc] init];
    textInfo.urlstr = URL_agreement;
    [self.navigationController pushViewController:textInfo animated:YES];
}

@end
